<?php
/**
 * Created by PhpStorm.
 * User: zhao
 * Date: 2018/5/22
 * Time: 13:43
 */

namespace App\Repositories;


use App\Models\GiftBuyUsed;
use App\Models\Mgift;
use App\Models\SaleInfo;

class OrderRepository extends BaseRepository
{
    /**
     * 获取解百旧系统相关卡号
     * @return mixed
     */
    public function getJbWxCardNo()
    {
        $hd_cards = $this->getSeesionWxuseMcard()->jbWxuserCard()->get();

        return $hd_cards->map(function ($item){
            return $item->card_no;
        })->toArray();
    }
    /**
     * 获取用户消费数据
     * @return array
     */
    public function getOrderDataByCardNo()
    {
        /*$card_nos = $this->getJbWxCardNo();

        return SaleInfo::whereIn ('custno',$card_nos)->get()->map(function($item){
            $details = explode('|',$item->detail);
            $arr_detail = [];
            foreach ($details as $value){
                $arr_detail[] = explode(',',$value);
            }

            $item->detail = $arr_detail;
            return $item;
        })->toArray();*/
        return $this->getSeesionWxuseMcard()->saleInfos()->get()->map(function($item){
            $details = explode('|',$item->detail);
            $arr_detail = [];
            foreach ($details as $value){
                $arr_detail[] = explode(',',$value);
            }

            $item->detail = $arr_detail;
            return $item;
        })->toArray();
    }


    /**
     * 根据小票号查找小票信息
     * @param $billno
     * @return mixed
     */
    public function getReceiptByBillno($billno)
    {
        return SaleInfo::where('billno',$billno)->first();
    }

    /**
     * 获取在线的可卖礼品卡
     * @return mixed
     */
    public function getOnlineMgifCardByMerchant()
    {
        return Mgift::where('type','elecard')
                    ->where('id_merchant',session('web_merchant')->id_merchant)
                    ->where('is_delete','N')
                    ->get();
    }

    /**
     * 获取用户购买礼品卡记录
     * @return mixed
     */
    public function getGiftCardByBuyUserId()
    {
        return $this->getSeesionWxuseMcard()->giftCardsBuy()->with('mgift')->get();
    }

    public function getUserBindGiftCard()
    {
        return $this->getSeesionWxuseMcard()->giftCardBind()->get();
    }

    /**
     * 获取礼品卡信息
     * @param $id
     * @return mixed
     */
    public function getGiftCardById($id)
    {
        return Mgift::where('id_gift',$id)
            ->where('id_merchant',session('web_merchant')->id_merchant)
            ->where('is_delete','N')
            ->first();
    }

    /**
     * 查询买礼品卡数据
     * @param $gift_no
     * @return mixed
     */
    public function getGiftBuyUsedByGiftNo($gift_no)
    {
        return GiftBuyUsed::where('gift_no',$gift_no)
                          ->where('status','Y')
                          ->first();
    }

}